package com.fh.controller;

import com.fh.model.DataTablePageBean;
import com.fh.model.DataTableResult;
import com.fh.model.User;
import com.fh.model.UserSearch;
import com.fh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author zhaolh
 * @title: StudentController
 * @projectName ssm-demo
 * @description: TODO
 * @date 2021/11/2 11:16
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("toUser")
    public String toUser(){
        return "user";
    }

    @RequestMapping("addUser")
    @ResponseBody
    public Map<String,Object> addUser(User user){
        Map<String,Object> map = new HashMap<>();
        try {
            userService.addUser(user);
            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
        }
        return map;
    }

    @RequestMapping("queryUserById")
    @ResponseBody
    public User queryUserById(Integer id){
        return userService.queryUserById(id);
    }

    @RequestMapping("select")
    @ResponseBody
    public DataTableResult queryUserList(DataTablePageBean pageBean, UserSearch search){
        DataTableResult dataTableResult = userService.queryUserList(pageBean,search);
        return dataTableResult;
    }

    @RequestMapping("delete")
    @ResponseBody
    public void deleteUser(Integer id){
        System.out.println("1111");
        System.out.println("222");
        userService.deleteUser(id);
    }

    @RequestMapping("update")
    @ResponseBody
    public void updateUser(User user){
        userService.updateUser(user);
    }

    @RequestMapping("deleteBatch")
    @ResponseBody
    public void deleteBatch(Integer[] ids){
        List list = new ArrayList();
        for (Integer id : ids) {
            list.add(id);
        }
        userService.deleteBatch(list);
    }
}
